package com.thinkfit.jy.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.common.lib.bean.ProceduralTrainBean;
import com.common.lib.bean.VideoListBean;
import com.common.lib.constant.Constants;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.utils.BaseUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.thinkfit.jy.R;
import com.thinkfit.jy.activity.ProceduraTrainDetailActivity;
import com.thinkfit.jy.adapter.ProceduralTrainingAdapter;
import com.thinkfit.jy.contract.ProceduralTrainingListContract;
import com.thinkfit.jy.presenter.ProceduralTrainingListPresenter;
import com.thinkfit.jy.view.SpaceItemDecoration;

public class ProceduralTrainingListFragment extends BaseFragment<ProceduralTrainingListContract.Presenter> implements ProceduralTrainingListContract.View, OnRefreshLoadMoreListener {

    ProceduralTrainingAdapter proceduralTrainingAdapter;
    int type;
    SpaceItemDecoration decoration;

    public static ProceduralTrainingListFragment getInstance(int index) {
        Bundle bundle = new Bundle();
        bundle.putInt(Constants.BUNDLE_EXTRA, index);
        ProceduralTrainingListFragment fragment = new ProceduralTrainingListFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @NonNull
    @Override
    protected ProceduralTrainingListContract.Presenter onCreatePresenter() {
        return new ProceduralTrainingListPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_video_list;
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {

        type = getArguments().getInt(Constants.BUNDLE_EXTRA);

        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(gridLayoutManager);
        getAdapter().onAttachedToRecyclerView(recyclerView);

        int screenWidth = BaseUtils.StaticParams.dp2px(getActivity(),790) - BaseUtils.StaticParams.dp2px(getActivity(),10)*2;// 23  recycleView 左右缩进距离
        int totalSpace=(screenWidth- BaseUtils.StaticParams.dp2px(getActivity(),220)*3);//  55 item宽度
        System.out.println("ddddddddddddddddddddddd  screenWidth : "+screenWidth+"  totalSpace : "+totalSpace);
        decoration = new SpaceItemDecoration(totalSpace,getActivity(),3);
        recyclerView.addItemDecoration(decoration);

        recyclerView.setAdapter(getAdapter());

        SmartRefreshLayout layout = view.findViewById(R.id.smartRefreshLayout);
        layout.setOnRefreshLoadMoreListener(this);
        layout.autoRefresh();
        layout.setEnableLoadMore(false);

        System.out.println("dddddddddddddddddddddddd  type :  "+type);
        getPresenter().getVideoList(type,0,0);
    }

    public ProceduralTrainingAdapter getAdapter(){
        if(proceduralTrainingAdapter == null){
            proceduralTrainingAdapter = new ProceduralTrainingAdapter(getContext());
        }
        proceduralTrainingAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable(Constants.BUNDLE_EXTRA, (VideoListBean) adapter.getData().get(position));
                openActivity(ProceduraTrainDetailActivity.class,bundle);
            }
        });


        return proceduralTrainingAdapter;
    }

    @Override
    public void onClick(View v) {

    }

    private void finishLoad() {
        SmartRefreshLayout layout = getView().findViewById(R.id.smartRefreshLayout);
        layout.finishLoadMore();
        layout.finishRefresh();
        layout.setEnableLoadMore(getAdapter().getItemCount() != 0
                && getAdapter().getItemCount() % 9 == 0);
        if (getAdapter().getItemCount() == 0) {
            setViewGone(R.id.recyclerView);
            setViewVisible(R.id.tvNoCourse);
        } else {
            setViewVisible(R.id.recyclerView);
            setViewGone(R.id.tvNoCourse);
        }
    }

    @Override
    public void getVideoListSuccess(ProceduralTrainBean bean) {
        getAdapter().setNewInstance(bean.getVideoList());
        finishLoad();
    }

    @Override
    public void getWattinfoSuccess(VideoListBean bean) {

    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

    }


}
